const util = require('../../../../../utils/util.js');
const api = require('../../../../../config/api.js');
const app = getApp();
Page({
  data: {
    //获取用户信息
    logininfo: {},
    //获取用户id
    openid: '',
    //获取访问用户id
    getopenid: '',
    //控制展开界面
    select: 1,
    //是否顶部固定
    fixed: false,
    //关注的人集合
    fansarr: [],
    //可能认识的人集合
    knowarr: [],
    //粉丝页数
    fansPageNum: 1,
    //可能认识的人页数
    knowPageNum: 1
  },
  //加载
  onLoad: function (option) {
    var _this = this
    _this.setData({
      logininfo: app.globalData.logininfo,
      openid: app.globalData.logininfo.openid,
      getOpenid: option.openid,
    });
    if (_this.data.openid == _this.data.getOpenid) {
      wx.setNavigationBarTitle({
        title: "我的粉丝",
        topText: "我"
      })
      _this.setData({
        topText: '我'
      })
    } else {
      wx.setNavigationBarTitle({
        title: "Ta的粉丝"
      })
      _this.setData({
        topText: 'Ta'
      })
    }
    var selectOpenid = _this.data.getOpenid
    var openid = _this.data.openid
    var data = {}
    if (openid != null && openid != "") {
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        data.openid = openid
        data.selectOpenid = selectOpenid
        data.access_token = res.access_token;
        data.pageNum = 1
        data.pageSize = 20
        util.post(api.GetUserFans, data).then(function (res) {
          if (res.code == 200) {
            _this.setData({
              fansarr: res.result.list,
              heatNum: res.result.list.length
            })
            console.log(res.result.list)
          }
        });
      });
    }
  },
  //获取页面监听
  onPageScroll: function (e) {
    var _this = this;
    var name = this.data.nickName;
    var scrollTop = parseInt(e.scrollTop);
    var query = wx.createSelectorQuery();
    if (scrollTop >= 1) {
      _this.setData({
        fixed: true
      })
    } else {
      _this.setData({
        fixed: false
      })
    }
  },
  //选择浏览的页面
  select: function (e) {
    var _this = this;
    var sId = e.currentTarget.dataset.id
    this.setData({
      select: sId
    })
    if (sId == 1) {
      var selectOpenid = _this.data.getOpenid
      var openid = _this.data.openid
      var data = {}
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid
          data.selectOpenid = selectOpenid
          data.access_token = res.access_token;
          data.pageNum = 1
          data.pageSize = 20
          util.post(api.GetUserFans, data).then(function (res) {
            if (res.code == 200) {
              _this.setData({
                fansarr: res.result.list,
                fansPageNum: 1
              })
              console.log(res.result.list)
            }
          });
        });
      }
    } else {
      var openid = _this.data.openid
      var data = {}
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid
          data.access_token = res.access_token;
          data.pageNum = 1
          data.pageSize = 20
          util.post(api.GetUserMayknow, data).then(function (res) {
            if (res.code == 200) {
              _this.setData({
                knowarr: res.result.list,
                knowPageNum: 1
              })
              console.log(res.result.list)
            }
          });
        });
      }
    }
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    }
  },
  //点击关注
  attention: function (e) {
    var eIndex = e.currentTarget.dataset.index
    var getId = e.currentTarget.dataset.id
    var type = e.currentTarget.dataset.type
    var _this = this;
    var openid = app.globalData.logininfo.openid;
    if (openid != null && openid != "") {
      var wechatopenid = e.currentTarget.dataset.wechatopenid;
      util.post(api.fetchToken()).then(function (res) {
        if (res.access_token != '') {
          return Promise.resolve({
            access_token: res.access_token
          });
        }
      }).then(function (res) {
        var data = {};
        data.wechatopenid = getId;
        data.openid = openid;
        data.access_token = res.access_token;
        util.post(api.Home_FocusOrCancelApiUrl, data).then(function (res) {
          if (res.code == 200) {
            var count = res.result.count;
            if (type == "know") {
              var arr = _this.data.knowarr
              arr[eIndex].focusCount = count
              _this.setData({
                knowarr: arr
              })
            } else {
              var arr = _this.data.fansarr
              arr[eIndex].focusCount = count
              _this.setData({
                fansarr: arr
              })
            }
          }
        })
      })
    }
  },
  //拉伸顶部刷新
  onPullDownRefresh: function () {
    var _this = this
    var sId = _this.data.select
    if (sId == 1) {
      var selectOpenid = _this.data.getOpenid
      var openid = _this.data.openid
      var data = {}
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid
          data.selectOpenid = selectOpenid
          data.access_token = res.access_token;
          data.pageNum = 1
          data.pageSize = 20
          util.post(api.GetUserFans, data).then(function (res) {
            if (res.code == 200) {
              _this.setData({
                fansarr: res.result.list,
                fansPageNum: 1
              })
              console.log(res.result.list)
            }
          });
        });
      }
    } else {
      var openid = _this.data.openid
      var data = {}
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid
          data.access_token = res.access_token;
          data.pageNum = 1
          data.pageSize = 20
          util.post(api.GetUserMayknow, data).then(function (res) {
            if (res.code == 200) {
              _this.setData({
                knowarr: res.result.list,
                knowPageNum: 1
              })
              console.log(res.result.list)
            }
          });
        });
      }
    }
    wx.stopPullDownRefresh();
  },
  //拉伸底部加载
  onReachBottom: function () {
    var _this = this
    var sId = _this.data.select
    if (sId == 1) {
      var num = parseInt(_this.data.fansPageNum + 1);
      var selectOpenid = _this.data.getOpenid
      var openid = _this.data.openid
      var data = {}
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid
          data.selectOpenid = selectOpenid
          data.access_token = res.access_token;
          data.pageNum = num
          data.pageSize = 20
          util.post(api.GetUserFans, data).then(function (res) {
            if (res.code == 200) {
              var detailsList = res.result.list;
              var arr = _this.data.fansarr;
              for (var i = 0; i <= detailsList.length - 1; i++) {
                arr.push(detailsList[i])
              }
              _this.setData({
                fansarr: arr,
                fansPageNum: num
              })
            }
          });
        });
      }
    } else {
      var num = parseInt(_this.data.knowPageNum + 1);
      var openid = _this.data.openid
      var data = {}
      if (openid != null && openid != "") {
        util.post(api.fetchToken()).then(function (res) {
          if (res.access_token != '') {
            return Promise.resolve({
              access_token: res.access_token
            });
          }
        }).then(function (res) {
          data.openid = openid
          data.access_token = res.access_token;
          data.pageNum = num
          data.pageSize = 20
          util.post(api.GetUserMayknow, data).then(function (res) {
            if (res.code == 200) {
              var detailsList = res.result.list;
              var arr = _this.data.knowarr;
              for (var i = 0; i <= detailsList.length - 1; i++) {
                arr.push(detailsList[i])
              }
              _this.setData({
                knowarr: arr,
                knowPageNum: num
              })
            }
          });
        });
      }
    }
    wx.stopPullDownRefresh();
  },

  gotohome: function (e) {
    console.log(e)
    var eId = e.target.dataset.id
    wx.navigateTo({
      url: '../personhome?openid=' + eId,
    })
  },
  gotoshouye: function () {
    app.switchData = 10;
    wx.switchTab({
      url: '/pages/home/home',
    })
  }
})